package com.cuitrip.business.login.ui.agreement;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.business.webview.proxy.WebViewProxy;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.span.NoLineClickSpan;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;

import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.Bind;

/**
 * 脆饼发现者协议控件
 * <p/>
 * Created by ak on 15/9/23.
 */
public class AgreementLayout extends FrameLayout {

    private Context mContext;

    private BaseActivity mActivity;

    public static final int REGISTER_MODE = 1;
    public static final int CREATE_SERVICE_MODE = 2;
    public static final int CREATE_ORDER_MODE = 3;

    @Bind(R.id.ct_agree_text)
    protected TextView agreeTextView;

   /* @Bind(R.id.ct_agreement)
    protected TextView agreement;*/

    @Bind(R.id.ct_agree_toggle)
    protected ToggleButton checkButton;

    private int mode = 0;

    public AgreementLayout(Context context) {
        super(context);
        initInternal(context);
    }

    public AgreementLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initInternal(context);
    }

    public AgreementLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initInternal(context);
    }

    private void initInternal(Context context) {
        mContext = context;
        mActivity = (BaseActivity) getContext();
        LayoutInflater.from(mContext).inflate(R.layout.layout_agreement, this);
        ButterKnife.bind(this);
        mActivity.setWithIconFont(checkButton);
        agreeTextView.setTextIsSelectable(false);
        checkButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (agreeCheckCallback != null) {
                    agreeCheckCallback.onAgreeCheckChanged(isChecked);
                }
            }
        });

    }

    private void bindUserAgreement(){
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
        String agreeTermService = getResources().getString(R.string.term_service_cn);
        String agreeText = ResourceUtil.getString(R.string.term_operation_agree, agreeTermService);
        int leftLength = agreeText.length() - agreeTermService.length();
        SpannableString agreeTextSpan = new SpannableString(agreeText);
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                checkButton.toggle();
            }
        }, 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getColor(R.color.qiaomai_7c706e)), 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //用户协议
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                TrackUtil.trackScreenName("/terms/service");
                WebViewProxy.getInstance().browseWeb(mActivity, H5UrlMaker.getUserAggreement());
            }
        },leftLength - 1, agreeText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), leftLength - 1, agreeText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spanBuilder.append(agreeTextSpan);

        agreeTextView.setText(spanBuilder);
        agreeTextView.setClickable(true);
        agreeTextView.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    private void bindTravelAndInsider(){
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
        String travellerTermService = getResources().getString(R.string.term_service_en_ft);
        String insiderTermService = getResources().getString(R.string.term_service_en_fi);
        String dot = ",";
        String agreementText = travellerTermService + dot + insiderTermService;
        String agreeText = ResourceUtil.getString(R.string.term_operation_agree, agreementText);
        int leftLength = agreeText.length() - agreementText.length();
        SpannableString agreeTextSpan = new SpannableString(agreeText);
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                checkButton.toggle();
            }
        }, 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getColor(R.color.qiaomai_7c706e)), 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //旅行者条款
        //int travellerStartIndex = leftLength - 1;
        int travellerStartIndex = leftLength ;
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                TrackUtil.trackScreenName("/terms/traveler");
                WebViewProxy.getInstance().browseWeb(mActivity, H5UrlMaker.getTravelAgreement());
            }
        },travellerStartIndex, travellerStartIndex + travellerTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if(I118Util.getLocale().equals(Locale.TRADITIONAL_CHINESE)){
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), travellerStartIndex, travellerStartIndex + travellerTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }else{
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), travellerStartIndex, travellerStartIndex + travellerTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }


        //发现者条款
        int insiderStartIndex = leftLength  + travellerTermService.length() + dot.length();
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                TrackUtil.trackScreenName("/terms/insider");
                WebViewProxy.getInstance().browseWeb(mActivity, H5UrlMaker.getInsiderAgreement());
            }
        }, insiderStartIndex, insiderStartIndex + insiderTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if(I118Util.getLocale().equals(Locale.TRADITIONAL_CHINESE)){
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), insiderStartIndex, insiderStartIndex + insiderTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }else{
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), insiderStartIndex, insiderStartIndex + insiderTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        spanBuilder.append(agreeTextSpan);
        agreeTextView.setText(spanBuilder);
        agreeTextView.setClickable(true);
        agreeTextView.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    private void bindInsider(){
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
//        String travellerTermService = getResources().getString(R.string.term_service_en_ft);
        String insiderTermService = getResources().getString(R.string.term_service_en_fi);
        String agreeText = ResourceUtil.getString(R.string.term_operation_agree, insiderTermService);
        int leftLength = agreeText.length() - insiderTermService.length();
        SpannableString agreeTextSpan = new SpannableString(agreeText);

        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                checkButton.toggle();
            }
        }, 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getColor(R.color.qiaomai_7c706e)), 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //发现者者条款
        int insiderStartIndex = leftLength - 1;
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                TrackUtil.trackScreenName("/terms/insider");
                WebViewProxy.getInstance().browseWeb(mActivity, H5UrlMaker.getInsiderAgreement());
            }
        },insiderStartIndex, insiderStartIndex + insiderTermService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if (I118Util.getLocale().equals(Locale.SIMPLIFIED_CHINESE)){
           agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()),insiderStartIndex,agreeText.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }else if(I118Util.getLocale().equals(Locale.TRADITIONAL_CHINESE)){
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()),insiderStartIndex,agreeText.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        } else{
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), insiderStartIndex, agreeText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        }


        spanBuilder.append(agreeTextSpan);
        agreeTextView.setText(spanBuilder);
        agreeTextView.setClickable(true);
        agreeTextView.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    private void bindTraveller(){
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
//        String travellerTermService = getResources().getString(R.string.term_service_en_ft);
        String termService = getResources().getString(R.string.term_service_en_ft);
        String agreeText = ResourceUtil.getString(R.string.term_operation_agree, termService);
        int leftLength = agreeText.length() - termService.length();
        SpannableString agreeTextSpan = new SpannableString(agreeText);

        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                checkButton.toggle();
            }
        }, 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getColor(R.color.qiaomai_7c706e)), 0, leftLength, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        //旅行者者条款
        int insiderStartIndex = leftLength - 1;
        agreeTextSpan.setSpan(new NoLineClickSpan() {
            @Override
            public void onClick(View widget) {
                TrackUtil.trackScreenName("/terms/traveler");
                WebViewProxy.getInstance().browseWeb(mActivity, H5UrlMaker.getTravelAgreement());
            }
        },insiderStartIndex, insiderStartIndex + termService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        if(I118Util.getLocale().equals(Locale.TRADITIONAL_CHINESE)){
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), insiderStartIndex, insiderStartIndex + termService.length()+1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        }else{
            agreeTextSpan.setSpan(new ForegroundColorSpan(ResourceUtil.getThemeColor()), insiderStartIndex, insiderStartIndex + termService.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        }


        spanBuilder.append(agreeTextSpan);
        agreeTextView.setText(spanBuilder);
        agreeTextView.setClickable(true);
        agreeTextView.setMovementMethod(new LinkMovementMethod() {
            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                super.onTakeFocus(view, text, dir);
            }
        });
    }

    private void bindRegisterMode(){
        if (I118Util.getLocale().equals(Locale.SIMPLIFIED_CHINESE)){
            bindUserAgreement();
        }else{
            bindTravelAndInsider();
        }
    }

    private void bindCreateServiceMode(){
        bindInsider();
    }

    private void bindCreateOrderMode(){
        if (I118Util.getLocale().equals(Locale.SIMPLIFIED_CHINESE)){
            bindUserAgreement();
        }else{
            bindTraveller();
        }
    }

    public void changeMode(int mode){
        this.mode = mode;
        if (mode == REGISTER_MODE){
            bindRegisterMode();
        }else if (mode == CREATE_ORDER_MODE){
            bindCreateOrderMode();
        }else if (mode == CREATE_SERVICE_MODE){
            bindCreateServiceMode();
        }
    }

    private AgreeCheckCallback agreeCheckCallback;

    public void setAgreeCheckCallback(AgreeCheckCallback agreeCheckCallback){
        this.agreeCheckCallback = agreeCheckCallback;
    }

    public boolean isChecked(){
        return checkButton.isChecked();
    }

    public interface AgreeCheckCallback{

        public void onAgreeCheckChanged(boolean isChecked);

    }

}
